﻿using KTD.Web.BLL;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XXS.Common;
using XXS.Common.ControllerExt;

namespace KTD.Web.Controllers
{
    public class AttachmentController : Controller
    {
        // GET: Attachment
        public ActionResult Index(AttachmentQD im)
        {
            ViewBag.Id = im.OSSId;
            var res = MF.AttachmentManager.GetListBy(im);
            return View(res);
        }


        public ActionResult Create(AttachmentCUD im)
        {
            try
            {
                var url = "";
                foreach (var item in im.File)
                {
                    im.SavePath = $"/Attachment/{Guid.NewGuid().ToString()}/{item.FileName}";
                    var combinePath = Server.MapPath($"~{im.SavePath }");

                    Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

                    item.SaveAs(combinePath);
                    //MF.AttachmentManager.Create(im);
                    url = url + im.SavePath + ",";
                }



                return this.JsonNet(new { succuess = true, url = url.TrimEnd(',') }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw ex;
            }
           
        }

        public ActionResult Delete(Guid Id)
        {
            try
            {
                MF.AttachmentManager.Delete(Id);
                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }

        }

        public ActionResult GetList(AttachmentQD im)
        {
            try
            {
                var res = MF.AttachmentManager.GetListBy(im);
                return this.JsonNet(res.ToExtGrid(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}